<?php
$imgpath=$_GET['img'];
if ($_GET['w'] != '' && $_GET['h'] != ''){
$initWidth	= $_GET['w'];
$initHeight	= $_GET['h'];
}else{
$initWidth	= 340;
$initHeight	= 270;
}
if(strpos($imgpath,'thumb.jpg')){
	$imgpath = substr($imgpath,0,-10);
}
 
 
$im  = @imagecreatefromjpeg($imgpath);
$imgSize	= GetImageSize($imgpath);
$imgWidth	= $imgSize[0];
$imgHeight	= $imgSize[1];
$proportion	= max($initWidth/$imgWidth,$initHeight/$imgHeight);
$newImgWidth	= $imgWidth*$proportion;
$newImgHeight	= $imgHeight*$proportion;
$tmpImg	= imagecreatetruecolor($newImgWidth,$newImgHeight);
imagecopyresized($tmpImg, $im, 0, 0, 0, 0, $newImgWidth, $newImgHeight, $imgWidth, $imgHeight);
 
$thumb = imagecreatetruecolor($initWidth, $initHeight);
$diffWidth	= $newImgWidth > $initWidth ? $newImgWidth - $initWidth : $initWidth - $newImgWidth;
$diffHeight	= $newImgHeight > $initHeight ? $newImgHeight - $initHeight : $initHeight - $newImgHeight;
 
$x	= $diffWidth/2;
//$y	= $diffHeight/3;
$y  = 0;
 
imagecopy($thumb, $tmpImg, 0, 0, $x, $y, $initWidth, $initHeight);
 
header('Content-type:image/jpeg');
// Output
imagejpeg($thumb);
ImageDestroy($thumb);
?>